# Transparent Corruption: The effect of illicit connections and trusted references 
# on the demand for bureaucratic intermediation

# Authors: Jose Ramon Morales-Arilla & Ana Gabriela Ibarra Luces

# SCRIPT 04 - ATTRITION ANALYSIS


# Data --------------------------------------------------------------------

# Generate variable of attrition

tabla_survey <- tabla_survey %>% 
  # Attrition before being assigned to a treatment branch or after
  dplyr::mutate(att_resp = ifelse(Progress == 9 | Progress == 58, 
                       yes = 1, no =0))


# ATTRITION REGRESSIONS ---------------------

# We perform regressions with attrition binary marker as a function of each 
# covariate of interest: corruption suggestion, price, reference, experience 
# and speed

att_6 <- feols(att_resp ~ Sug_Corr, data = tabla_survey, vcov = "hetero")

att_7 <- feols(att_resp ~ Precio, data = tabla_survey, vcov = "hetero")

att_8 <- feols(att_resp ~ GestLink, data = tabla_survey, vcov = "hetero")

att_9 <- feols(att_resp ~ Exper, data = tabla_survey, vcov = "hetero")

att_10 <- feols(att_resp ~ Rapidez, data = tabla_survey, vcov = "hetero")



# TABLE  ------------------------------------------------------------------

# Assembling table of the differences in proportions of attrition between 
# those who received treatment and those who did not (Beta_1) after they
# being assigned to a treatment branch

attrition_table <- huxreg("Corruption" =att_6, "Price" =att_7, "Reference" =att_8, "Experience" =att_9, "Speed" =att_10,
                  statistics = c("N. obs." = "nobs", 
                                 "R squared" = "r.squared", "F statistic" = "statistic",
                                 "P value" = "p.value"), 
                  coefs = c("Attrition" = "Precio", 
                            'Attrition' = "Sug_Corr",
                            "Attrition"="GestLink", 
                            "Attrition" = "Exper", 
                            "Attrition" = "Rapidez"), 
                  stars = c(`*` = 0.1, `**` = 0.05, `***` = 0.01))
# EXPORT ------------------------------------------------------------------

write_xlsx(attrition_table, 'Data/Output/Tables/04_attrition_analysis_table.xlsx')

print(xtable(attrition_table, type = "latex"), include.colnames = F, include.rownames = F, file = "Data/Output/Tables/04_attrition_analysis_table.tex")

# ## ----------------------------------------------------------------------

rm(list = setdiff(ls(), c('tabla_survey', 'tab_1', 'tab_2', 'tab_3', 'tab_4', 
                          'tab_1_q', 'tab_2_q', 'tab_3_q', 'tab_h', 'tab_sq', 
                          'reg_1_c', 'reg_5_c', 'reg_9_c', 'reg_1_s', 'reg_1_h', 
                          'attrition_table')))
